
	var colorValue = "";
    var proInfo;
    var proStr;
    var existReturn;
	function myInnerHTML(id,data){
		document.getElementById(id).innerHTML = data;
	}

	/**
	 *@Author cirly
	 *@Description 根据productQR获取指定商品信息
	 *@Date 12:55 2018/11/5
	 *@Modify by
	 *@Version 1.0
	 */
	window.onload=function (){
        //document.getElementById("input-num").value = "1";
        var para = location.search;
        var theRequest = {};
        if (para.indexOf("?") != -1) {
            var str = para.substr(1);
            strs = str.split("&");
            for(var i = 0; i < strs.length; i ++) {
                theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
            }
        }
        var productQR = theRequest.productQR;

        //alert(productQR);

		var url = "productInfo!getById";

		var data = "productQR=" + productQR;

		sendRequest("post",url,data,getBack);


	};

	//商品信息回调
	function getBack(result){
		//var json = [];
		var rs = eval("(" + result + ")") ;
		rs = eval("(" + eval("(" + result + ")") + ")");
		var obj = rs;//转换后的JSON对象
		proInfo = obj;
		proStr = rs;
		var str = "";
			str += "<div class=\"swiper-slide\"><img src=\"data:image/jpg;base64,"+ obj.productPhoto+"\" alt=\"\"></div>";

        $(".zyw-container .item-img .swiper-wrapper").html(str);

		myInnerHTML("introduction",obj.introduction);
		myInnerHTML("rentPrice",obj.rentPrice);
		myInnerHTML("rentPrice2",obj.rentPrice);
		myInnerHTML("buyPrice",obj.salePrice);
        myInnerHTML("remainder",obj.productNumber);
		myInnerHTML("buyPrice2",obj.salePrice);
		myInnerHTML("productFactory",obj.productFactory);
		myInnerHTML("kucun",obj.productNumber);

		$(".spec-head-img").html("<img src=\"data:image/jpg;base64,"+ obj.productPhoto+"\" alt=\"\">");
		if(obj.salePrice == 0){
			$("#purchase").addClass("disabled");
            $("#purchase").attr("disabled",true);
		}
		if(obj.rentPrice == 0){
			$("#rent").addClass("disabled");
            $("#rent").attr("disabled",true);
		}

	}

	function change(data){
		$(data).addClass("active").siblings().removeClass("active");

		colorValue = data.innerHTML;

		//alert(colorValue);
	}

	function minus(){
		var num = parseInt(document.getElementById("input-num").value);
		if(num>0)

			document.getElementById("input-num").value = num-1;
	}

	function add() {
        var num = parseInt(document.getElementById("input-num").value);
        var n = num + 1;                   //点击加号之后的数量
        var productNumber = parseInt(document.getElementById("kucun").innerHTML);         //商品库存
        if (n > productNumber) {
            alert("库存仅剩" + productNumber + "件！");
        } else {
            document.getElementById("input-num").value = num + 1;
        }
    }
	function info(){
		if(colorValue = " "){
			colorValue = $("ul li").first().html();
		}
			var num = parseInt(document.getElementById("input-num").value);
		document.getElementById("colorspan").innerHTML = "已选数量:"+num;
	}

	/**
	 *@Author cirly
	 *@Description  if(登录) then 购买 else 跳转登录
	 *@Date 12:56 2018/11/5
	 *@Modify by
	 *@Version 1.0
	 */
	function buyConfirm(){
		var buyNum = parseInt(document.getElementById("input-num").value);     //购买数量
		var remainder = parseInt(document.getElementById("kucun").innerHTML);  //余量
		if(buyNum == 0 || buyNum == undefined){
            alert("请选择商品数量！")
        }else if (buyNum > remainder){
			alert("库存不足！请确认数量");

		}else{
            var customer = JSON.parse(localStorage.getItem("customer"));
            if (customer == null) {
            	alert('请登录后操作！');
                window.location.href = "../login/login.html"
            } else {
            	isExist(proInfo.productQR);
            }
        }
	}

    /**
     *@Author cirly
     *@Description get callback of insert
     *@Date 19:47 2019/3/19
     *@Modify by 
     *@Version 1.0
     */	  
    function insertShoppingBack(result){
        var rs =  eval('(' + result + ')');
        if(rs == "insert_success") {
            alert("购物车添加成功！");
           window.location.href = 'shoppingCart.html';
		}else{
        	alert("购物车添加失败！");
		}
	}
	
	/**
	 *@Author cirly
	 *@Description: judge whether the shopping cart already exists before adding the shopping cart
     *@Date 13:20 2019/3/24
	 *@Modify by 
	 *@Version 1.0
	 */
	function isExist(productQR){
        var url = "/shoppingCart!isExist";
        var data = "productQR="+productQR;
        sendRequest("post",url,data,isExistBack);
	}

	function isExistBack(result){
        var rs  = eval('(' + result + ')');
        if(rs == "exist"){
            alert("购物车已有此商品！");
            return ;
        }else{
            customer = JSON.parse(localStorage.getItem("customer"));
            var url = "shoppingCart!insertShoppingCart";
            //alert(customer.accountNo);
			//alert(parseInt(document.getElementById("input-num").value));
            var data = "accountNo=" + customer.accountNo+"&custPhone="+customer.custPhone
                +"&productQR="+proInfo.productQR+"&buyAmount="+parseInt(document.getElementById("input-num").value);
            console.log(data);
            sendRequest("post",url,data,insertShoppingBack);
        }
	}


	function rentConfirm(){
		//proStr = eval("("+ proInfo +")");
		proStr = JSON.stringify(proInfo);

        var customer = localStorage.getItem("customer");
        if (customer == null) {
            window.location.href = "/login/login.html"
        } else {
            localStorage.setItem("proStr",proStr);
            window.location.href = "addRentCart.html";
        }


		//console.log(proStr);
	}
